package com.easylinkin.linkappapi.lobar.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.dto.VisitorRecordDTO;
import com.easylinkin.linkappapi.lobar.entity.VisitorRecord;
import com.easylinkin.linkappapi.lobar.mapper.VisitorRecordMapper;
import com.easylinkin.linkappapi.lobar.service.VisitorRecordService;
import com.easylinkin.linkappapi.lobar.vo.VisitorRecordStatusVO;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 来访人员记录 服务实现类
 * </p>
 *
 * @author Xiaoyang
 * @since 2022-12-13
 */
@Service
public class VisitorRecordServiceImpl extends ServiceImpl<VisitorRecordMapper, VisitorRecord> implements VisitorRecordService {

  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;

  @Override
  public void add(VisitorRecord visitorRecord) {
    visitorRecord.setStatus(0);
    visitorRecord.setInTime(new Date());
    visitorRecord.setCreateTime(new Date());
    visitorRecord.setModifyTime(new Date());
    baseMapper.insert(visitorRecord);
  }

  @Override
  public IPage<VisitorRecord> listPage(RequestModel<VisitorRecordDTO> requestModel) {
    Page page = requestModel.getPage();
    VisitorRecordDTO visitorRecord = requestModel.getCustomQueryParams();
    //项目id 过滤
    visitorRecord.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    IPage<VisitorRecord> listPage = baseMapper.listPage(page,visitorRecord);
    return listPage;
  }

  @Override
  public void visitorOut(String id) {
    VisitorRecord old = baseMapper.selectById(id);
    if (null != old){
      UpdateWrapper<VisitorRecord> updateWrapper = new UpdateWrapper<>();
      updateWrapper.eq("id",id);
      old.setOutTime(new Date());
      old.setStatus(1);
      old.setModifyTime(new Date());
      old.setModifyId(linkappUserContextProducer.getCurrent().getId());
      baseMapper.update(old,updateWrapper);
    }
  }

  @Override
  public List<VisitorRecordStatusVO> countByStatus(VisitorRecordDTO visitorRecordDTO) {
    //项目id 过滤
    visitorRecordDTO.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    List<VisitorRecordStatusVO> visitorRecordStatusVOS = baseMapper.countByStatus(visitorRecordDTO);
    return visitorRecordStatusVOS;
  }
}
